/*
 * MoneyAndDebitDetail.java
 *
 * Created on 2008年5月27日, 下午10:06
 */

package gui;
import java.util.Vector;
import abstractmodel.IDetail;
import java.util.ArrayList;
import model.Detail;
import model.MoneyAccount;
import model.BankAccount;
import model.BankAccountInfo;
import abstractmodel.IAccount;
import abstractmodel.IInfo;
import abstractmodel.InfoValue;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  yux
 */
public class MoneyAndDebitDetail extends javax.swing.JPanel {
    private int column=model.DetailInfo.titles.length;;
    private int type;
    private BankAccount account1;
    private MoneyAccount account2;
    private String[] store;
    private int init = 0;
    /** Creates new form MoneyAndDebitDetail */
    public MoneyAndDebitDetail(IAccount a) {
        if (a.getAccountInfo().getType() == configure.Global.BANK_ACCOUNT_INFO_TYPE) {
            account1 = (BankAccount)a;
            type = configure.Global.BANK_ACCOUNT_INFO_TYPE;
        }else if (a.getAccountInfo().getType() == configure.Global.MONEY_ACCOUNT_INFO_TYPE) {
            account2 = (MoneyAccount)a;
            type = configure.Global.MONEY_ACCOUNT_INFO_TYPE;
        }
        initComponents();
        javax.swing.table.DefaultTableModel md = 
                new javax.swing.table.DefaultTableModel(model.DetailInfo.titles, 0);
        this.jTableDetail.setModel(md);
        initTable();
        this.jTableDetail.setAutoCreateRowSorter(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableDetail = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanelSet = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldDate = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldReason = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldAmount = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldLeave = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldOther = new javax.swing.JTextField();
        jButtonConfirm = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        jTableDetail.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "收入/支出日期", "原因", "交易金额", "帐户余额", "备注"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableDetail.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableDetailMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableDetail);

        jPanel1.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 639, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 224, Short.MAX_VALUE)
        );

        jPanel1.add(jPanel2, "card2");

        jLabel1.setText("收支日期");

        jLabel2.setText("原因");

        jLabel3.setText("数额");

        jLabel4.setText("余额");

        jLabel5.setText("备注");

        jButtonConfirm.setText("确定");
        jButtonConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmActionPerformed(evt);
            }
        });

        jButton1.setText("删除");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelSetLayout = new javax.swing.GroupLayout(jPanelSet);
        jPanelSet.setLayout(jPanelSetLayout);
        jPanelSetLayout.setHorizontalGroup(
            jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSetLayout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelSetLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldLeave, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))
                    .addGroup(jPanelSetLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldDate, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(22, 22, 22)
                .addGroup(jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelSetLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldOther, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanelSetLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldReason, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel3)
                        .addGap(19, 19, 19)
                        .addComponent(jTextFieldAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(96, 96, 96))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelSetLayout.createSequentialGroup()
                .addContainerGap(418, Short.MAX_VALUE)
                .addComponent(jButtonConfirm)
                .addGap(27, 27, 27)
                .addComponent(jButton1)
                .addGap(84, 84, 84))
        );
        jPanelSetLayout.setVerticalGroup(
            jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSetLayout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldReason, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(40, 40, 40)
                .addGroup(jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldLeave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldOther, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(jPanelSetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonConfirm)
                    .addComponent(jButton1))
                .addGap(33, 33, 33))
        );

        jPanel1.add(jPanelSet, "card3");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 639, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 639, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTableDetailMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableDetailMouseClicked
        // TODO add your handling code here:
        java.awt.CardLayout cl = (java.awt.CardLayout)jPanel1.getLayout();
        if (init == 0) {
            init = 1;
            cl.next(jPanel1);
        }
        int index = jTableDetail.getSelectedRow();
        if(index<0)
            return;
        ArrayList<IDetail> detailList;
        if (type == configure.Global.BANK_ACCOUNT_INFO_TYPE) {
            detailList = (ArrayList<IDetail>)account1.getDetailsInfo();
        }else if (type == configure.Global.MONEY_ACCOUNT_INFO_TYPE){
            detailList = (ArrayList<IDetail>)account2.getDetailsInfo();
        }else {
            System.out.println("点击的传入的账本类型错误！");
            return;
        }
        Detail detail = (Detail)detailList.get(index);
        jTextFieldDate.setText(detail.getDetailInfo().getValue("收支日期").getString());
        jTextFieldAmount.setText(detail.getDetailInfo().getValue("数额").getString());
        jTextFieldLeave.setText(detail.getDetailInfo().getValue("余额").getString());
        jTextFieldReason.setText(detail.getDetailInfo().getValue("原因").getString());
        jTextFieldOther.setText(detail.getDetailInfo().getValue("备注").getString());
        store = new String[column];
        store[0] = jTextFieldDate.getText();
        store[1] = jTextFieldAmount.getText();
        store[2] = jTextFieldLeave.getText();
        store[3] = jTextFieldReason.getText();
        store[4] = jTextFieldOther.getText();
    }//GEN-LAST:event_jTableDetailMouseClicked

    private void jButtonConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmActionPerformed
        // TODO add your handling code here:
        int index = jTableDetail.getSelectedRow();
        ArrayList<IDetail> detailList;
        if (type == configure.Global.BANK_ACCOUNT_INFO_TYPE) {
            detailList = (ArrayList<IDetail>)account1.getDetailsInfo();
        }else if (type == configure.Global.MONEY_ACCOUNT_INFO_TYPE){
            detailList = (ArrayList<IDetail>)account2.getDetailsInfo();
        }else {
            System.out.println("点击的传入的账本类型错误！");
            return;
        }
        Detail detail = (Detail)detailList.get(index);
        IInfo info=detail.getDetailInfo();
        int fault = 0;
        if (!info.setValue("收支日期", jTextFieldDate.getText()))
            fault = 1;
        else if (!info.setValue("数额", jTextFieldAmount.getText()))
            fault = 2;
        else if (!info.setValue("余额", jTextFieldLeave.getText()))
            fault = 3;
        else if (!info.setValue("原因", jTextFieldReason.getText()))
            fault = 4;
        else if (!info.setValue("备注", jTextFieldOther.getText()))
            fault = 5;
        //setback
        if (fault >= 1)
            jTextFieldDate.setText(store[0]);
        if (fault >= 2)
            jTextFieldAmount.setText(store[1]);
        if (fault >= 3)
            jTextFieldLeave.setText(store[2]);
        if (fault >= 4)
            jTextFieldReason.setText(store[3]);
        if (fault >= 5)
            jTextFieldOther.setText(store[5]);
        //alarm
        if (fault != 0) {
            String[] options = {"确定"};
            javax.swing.JOptionPane.showOptionDialog(null, "更新明细失败，请重新更新！", 
                    "警告", javax.swing.JOptionPane.DEFAULT_OPTION, 
                    javax.swing.JOptionPane.WARNING_MESSAGE, null, options, options[0]); 
        }
        detail.setDetailInfo(info);
        initTable();
        jTableDetail.clearSelection();
        jTableDetail.addRowSelectionInterval(index, index);
    }//GEN-LAST:event_jButtonConfirmActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        int index = jTableDetail.getSelectedRow();
        if(index<0)
            return;
        ArrayList<IDetail> detailList;
        if (type == configure.Global.BANK_ACCOUNT_INFO_TYPE) {
            detailList = (ArrayList<IDetail>)account1.getDetailsInfo();
        }else if (type == configure.Global.MONEY_ACCOUNT_INFO_TYPE){
            detailList = (ArrayList<IDetail>)account2.getDetailsInfo();
        }else {
            System.out.println("点击的传入的账本类型错误！");
            return;
        }
        Detail detail = (Detail)detailList.get(index);
        if (type == configure.Global.BANK_ACCOUNT_INFO_TYPE) {
           account1.deleteDetailInfo(detail);
        }else if (type == configure.Global.MONEY_ACCOUNT_INFO_TYPE){
           account2.deleteDetailInfo(detail);
        }
        initTable();
        jTableDetail.clearSelection();
        //this.getParent().removeAll();
        ((JPanel)this.getParent()).updateUI();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void initTable() {
        //jTableDetail.setModel(md);
        DefaultTableModel md=(DefaultTableModel) jTableDetail.getModel();
        md.setNumRows(0);
        ArrayList<IDetail> detailList;
        if (type == configure.Global.BANK_ACCOUNT_INFO_TYPE) {
            detailList = (ArrayList<IDetail>)account1.getDetailsInfo();
        }else if (type == configure.Global.MONEY_ACCOUNT_INFO_TYPE){
            detailList = (ArrayList<IDetail>)account2.getDetailsInfo();
        }else {
            System.out.println("传入的账本类型错误！");
            return;
        }       
        int number = detailList.size();
        for (int i = 0; i < number; i++) {
            Detail detail = (Detail)detailList.get(i);
            Vector show = new Vector(column);
            for (int j = 0; j < column; j++)  {
                InfoValue infov=detail.getDetailInfo().getValue(model.DetailInfo.titles[j]);
                Object o=null;
                if(infov.getType()==Double.class) {
                    o=new Double(infov.getDouble());
                }else {
                    o=infov.getString();
                }
                show.add(o);
            }
            md.addRow(show);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonConfirm;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelSet;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableDetail;
    private javax.swing.JTextField jTextFieldAmount;
    private javax.swing.JTextField jTextFieldDate;
    private javax.swing.JTextField jTextFieldLeave;
    private javax.swing.JTextField jTextFieldOther;
    private javax.swing.JTextField jTextFieldReason;
    // End of variables declaration//GEN-END:variables
    
}
